$(function () {
    $("#emp_datagrid").datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#emp_toolbar',
        url: '/employee/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '用户名', field: 'username',align:'center', width: '500'},
            {title: '真实姓名', field: 'realname' ,align:'center', width: '500'},
            {title: '电话', field: 'tel' ,align:'center', width: '500'},
            {title: '邮箱', field: 'email' ,align:'center', width: '500'},
            {
                title: '部门', field: 'dept' ,align:'center', width: '500', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {title: '入职日期', field: 'hireDate' ,align:'center', width: '500'},
            {
                title: '状态', field: 'state' ,align:'center', width: '500', formatter: function (value, row, index) {
                    return value ? "<font color='green'>在职</font>" : "<font color='red'>离职</font>"
                }
            },
            {
                title: '管理员', field: 'admin' ,align:'center', width: '500', formatter: function (value, row, index) {
                    return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                }
            }
        ]],
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: "离职"
                })
            } else {
                $("#btn_state").linkbutton({
                    text: "复职"
                })
            }
        }
    })

    $("#emp_dialog").dialog({
        width: 350,
        height: 470,
        buttons: '#emp_buttons',
        closed: true,
        onClose: function () {
            $("#emp_form").form("clear")
        }
    })

    $("#emp_changePwd").dialog({
        width: 300,
        height: 200,
        title : '重置密码',
        buttons: '#emp_btn_Pwd',
        closed: true,
        onClose: function () {
            $("#emp_pwdForm").form("clear")
        }
    })

    var methodObj = {
        //添加按钮
        add: function () {
            $("#emp_dialog").dialog("open");
            $("#tr_password").show();
            $("#emp_dialog").dialog('setTitle', '新增员工');
        },
        //编辑按钮
        edit: function () {
            var data = $("#emp_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $("#tr_password").hide();

            if (data.dept) {
                data['dept.id'] = data.dept.id;
            }
            $("#emp_form").form('load', data);

            $.get("/role/selectRoleByEmpId.do",{id:data.id},function (ids) {
                $("#roles").combobox("setValues",ids);
            })

            $("#emp_dialog").dialog("open");
            $("#emp_dialog").dialog('setTitle', '编辑员工');
        },
        //离职按钮
        status: function () {
            var row = $("#emp_datagrid").datagrid("getSelected");
            var text = $("#btn_state").text();
            if (!row) {
                $.messager.alert("温馨提示", "请选择要设置的数据", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要设置" + text + "吗？", function (r) {
                if (r) {
                    $.get("/employee/status.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "设置成功", "info", function () {
                                $("#emp_datagrid").datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })
        },
        //刷新按钮
        reload: function () {
            $("#emp_datagrid").datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            $("#emp_dialog").dialog("close");
            $("#emp_changePwd").dialog("close");
        },
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            var qoDept = $("#qoDept").textbox("getValue");

            $("#emp_datagrid").datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate,
                deptId: qoDept
            });
        },
        //添加保存按钮
        save: function (val) {
            var url = $(val).data("url");
            var from = $(val).data("from");
            $("#" + from).form('submit', {
                url: '/employee/'+url+'.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            $("#emp_datagrid").datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                },
                onSubmit:function (param) {
                    var ids = $("#roles").combobox("getValues");
                    $(ids).each(function (index,val) {
                        param["roles["+index+"].id"] = val;
                    })
                }
            })
        },
        //更改密码
        changePwd : function () {
            var data = $("#emp_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $("#emp_pwdForm").form('load', data);

            $("#emp_changePwd").dialog("open");
        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

})

